/*
 * This is the Socket in thread for Socket Server
 */
package SocketDemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class SocketThread extends Thread {
    private Socket socket;

    public SocketThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
            String temp = null;
            String info = "";
            while ((temp = reader.readLine()) != null) {
                info += temp;
                System.out.println("Server:     Recv MSG from " + socket.getInetAddress().getHostAddress() + ": " + info);
            }
            writer.print("Received the MSG");
            writer.flush();
            socket.shutdownOutput();
            writer.close();
            reader.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
